
/*
TimerCallback_1.pde -- Uses user-written functions outside of loop() --first example with fixed speeds.

Copyright (c) 2010 Ed Bennett.  All rights reserved. 

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

//
// using user-written functions outside of loop() --first example with fixed speeds
//
// Three LED's blinking together at different rates.
// Three timer channels are used. The channels are 
// numbered 0, 1, and 2. 
//
// Timer commands usually take a channel number and/or a 
// time-value. 
//
// The unit of time is is called a blink. The duration 
// of a blink is related to an AVR jiffy. See
// http://en.wikipedia.org/wiki/Jiffy_%28time%29
//
// This example uses a callback functions that you write. Your
// function is registered with a timer. When the timer fires off,
// it runs your function.
//
// Your callback functions should be very short and very fast. Don't
// use delay() or anything else that eats a lot of time. Serial.print()
// is very slow, for example. While your callback is running, the chip is 
// stuck on a very low level that affects other things. It's a good
// idea to add a blinking LED to your program just so that you can see
// whether it locks up due to a callback that runs too slowly.

#include <TimerScheduler.h>

// hardware pins
int LED0 = 11;
int LED1 = 10;
int LED2 = 9;

int outval0 = 0;
int outval1 = 0;
int outval2 = 0;


void setup()
{   
  pinMode(LED0, OUTPUT); 
  pinMode(LED1, OUTPUT); 
  pinMode(LED2, OUTPUT);

  Timer.begin();        // start the hardware timer
  
  // Register user's callback function name with a timer channel.
  // blinkLED0 is a function I wrote further down this page.
 
  Timer.onTick(0, blinkLED0); //(channelNumber, functionName)
  Timer.onTick(1, blinkLED1); 
  Timer.onTick(2, blinkLED2);

  // time interval
  Timer.repeat(0,1501); // (channel, time)
  Timer.repeat(1,700); 
  Timer.repeat(2,22);
  
  Timer.start(0);       // (channel)
  Timer.start(1); 
  Timer.start(2); 
}


// You can put things in loop(), if you like,
// but it's not required for the callbacks.

void loop()
{
}

// here are the user's callbacks

void blinkLED0(void){
    outval0 ^= 1;          // toggle the pin
    digitalWrite(LED0, outval0);
}

void blinkLED1(void){
    outval1 ^= 1;
    digitalWrite(LED1, outval1);
}

void blinkLED2(void){
    outval2 ^= 1;
    digitalWrite(LED2, outval2);
}

